package com.topisv.tms.web;

import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.GoodsShelf;
import com.topisv.tms.entity.Storage;
import com.topisv.tms.entity.StoragePartition;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.GoodsShelfService;
import com.topisv.tms.service.StoragePartitionService;
import com.topisv.tms.service.StorageService;

/**
 * 货架管理的Controller, 使用Restful风格的Urls:
 * 
 * 
 * @author Arthur
 */
@Controller
@RequestMapping(value = "/storagePartition")
public class StoragePartitionController {


	@Autowired
	private StoragePartitionService storagePartitionService;
	
	@RequestMapping(value = "")
	public String list(	Model model, ServletRequest request) {
		List<StoragePartition> storagePartitions = storagePartitionService.getAllStoragePartition();
		model.addAttribute("storagePartitions", storagePartitions);
		return "storagepartition/storagePartitionList";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("storagePartition", new StoragePartition());
		model.addAttribute("action", "create");
		return "storagepartition/storagePartitionForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid StoragePartition storagePartition, RedirectAttributes redirectAttributes) {
		storagePartitionService.saveStoragePartition(storagePartition);
		redirectAttributes.addFlashAttribute("message", "创建货架成功");
		return "redirect:/goodsShelf/";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		
		model.addAttribute("action", "create");
		return "storagepartition/storagePartitionForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("storage") GoodsShelf goodsShelf,RedirectAttributes redirectAttributes) {
		
		redirectAttributes.addFlashAttribute("message", "修改货架成功");
		return "redirect:/goodsShelf/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		storagePartitionService.deleteStoragePartition(id);
		redirectAttributes.addFlashAttribute("message", "删除货架成功");
		return "redirect:/goodsShelf/";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadStoragePartition")
	public StoragePartition getStoragePartition(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return storagePartitionService.getStoragePartition(id);
		}
		return null;
	}
}